package com.appkubes.fintech.admin.application.user.controller;

import com.appkubes.fintech.admin.application.bill.service.RentBillHeaderService;
import com.appkubes.fintech.admin.application.credit.service.CreditService;
import com.appkubes.fintech.admin.application.risk.data.boole.service.BooleRiskService;
import com.appkubes.fintech.admin.application.user.dao.UserDao;
import com.appkubes.fintech.admin.application.user.dao.UserPersonalDao;
import com.appkubes.fintech.admin.application.user.entity.dto.UserBaseInfoDTO;
import com.appkubes.fintech.admin.application.user.entity.dto.UserCertificateStatusDTO;
import com.appkubes.fintech.admin.application.user.entity.dto.UserDTO;
import com.appkubes.fintech.admin.application.user.entity.vo.UserApprovalVO;
import com.appkubes.fintech.admin.application.user.entity.vo.UserBaseInfoVO;
import com.appkubes.fintech.admin.application.user.service.UserBaseInfoService;
import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.core.po.FintechCredit;
import com.appkubes.fintech.core.po.FintechUser;
import com.appkubes.fintech.core.po.FintechUserPersonal;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/*
 *
 * @author xqc
 * @date 	2020/01/01 14:46:28
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户基本信息", value = "FintechUserBaseInfoController")
@Slf4j
public class UserBaseInfoController {

    @Autowired
    private UserBaseInfoService userBaseInfoService;
    @Resource
    BooleRiskService booleRiskService;
    @Resource
    private UserDao userDao;
    @Resource
    private UserPersonalDao personalDao;

    @Resource
    private CreditService creditService; //信用额度服务

    @Resource
    private RentBillHeaderService rentBillHeaderService;

    @ApiOperation(tags = "分页用户基本信息", value = "queryPage")
    @RequestMapping(value = "queryPage")
    public Response<PageInfo<UserBaseInfoDTO>> queryPage(@RequestBody UserBaseInfoVO userBaseInfoVO) {
        PageInfo<UserBaseInfoDTO> pageInfo = userBaseInfoService.queryAuthPage(userBaseInfoVO);
        return Response.success(pageInfo);
    }

    @ApiOperation(tags = "分页认证用户信息", value = "queryAuthPage")
    @RequestMapping(value = "queryAuthPage")
    public Response<PageInfo<UserBaseInfoDTO>> queryAuthPage(@RequestBody UserBaseInfoVO userBaseInfoVO) {
        PageInfo<UserBaseInfoDTO> pageInfo = userBaseInfoService.queryAuthPage(userBaseInfoVO);
        return Response.success(pageInfo);
    }

    @ApiOperation(tags = "获取用户基本信息", value = "getById")
    @GetMapping(value = "getById")
    public Response<UserBaseInfoDTO> getById(String id) {
        return userBaseInfoService.getById(id);
    }

    @ApiOperation(tags = "获取用户基本信息", value = "getById")
    @GetMapping(value = "getAuthId")
    public Response<UserDTO> getAuthId(String id) {
        return userBaseInfoService.getAuthId(id);
    }


    @ApiOperation(tags = "额度审批", value = "approval")
    @PostMapping(value = "approval")
    public Response approval(@RequestBody UserApprovalVO vo) {

        return userBaseInfoService.approval(vo);
    }

    @ApiOperation(tags = "删除用户信息", value = "remove")
    @PostMapping(value = "remove")
    public Response deleteUserInfo(@RequestBody UserApprovalVO vo) {
        creditService.deleteUserInfo(vo.getUserKey());
        return Response.success();
    }

    @ApiOperation(tags = "复活用户", value = "revert")
    @PostMapping(value = "revert")
    public Response revertUser(@RequestBody UserApprovalVO vo) {
        creditService.revertUser(vo.getUserKey());
        return Response.success();
    }

    @ApiOperation(tags = "修改额度", value = "editCredit")
    @PostMapping(value = "editCredit")
    public Response editCredit(@RequestBody UserApprovalVO vo) {
        creditService.editCredit(vo);
        return Response.success();
    }

    @ApiOperation("获取前置租赁风险")
    @GetMapping("/getPreRisk")
    public Response getPreRisk(@RequestParam("userKey") String userKey) {
        Response response = booleRiskService.getPreRisk(userKey);
        Response<UserBaseInfoDTO> baseInfoDTO = userBaseInfoService.getById(userKey);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("baseInfo", baseInfoDTO.getData());
        map.put("riskInfo", "");
        if (response.getCode() == 200) {
            map.put("riskInfo", response.getData());
        }
        return Response.success(map);

    }

    @ApiOperation("获取预言家报告")
    @GetMapping("/getProphetReport")
    public Response getProphetReport(@RequestParam("userKey") String userKey) {
        Response response = booleRiskService.getProphetReport(userKey);
        Response<UserBaseInfoDTO> baseInfoDTO = userBaseInfoService.getById(userKey);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("baseInfo", baseInfoDTO.getData());
        map.put("riskInfo", "");
        if (response.getCode() == 200) {
            map.put("riskInfo", response.getData());
        }
        return Response.success(map);
    }


    @GetMapping("/getCertificateStatus")
    @ApiOperation("用户认证状态信息")
    public Response<UserCertificateStatusDTO> getCertificateStatus(String userKey) {

        UserCertificateStatusDTO userCertificateStatusDTO = new UserCertificateStatusDTO();
        FintechUser fintechUser = userDao.selectByPrimaryKey(userKey);
        if (fintechUser != null) {
            BeanUtils.copyProperties(fintechUser, userCertificateStatusDTO);
        }
        FintechUserPersonal userPersonal = personalDao.selectByPrimaryKey(userKey);
        if (userPersonal != null) {
            userCertificateStatusDTO.setIdcard_back_view(userPersonal.getIdcardBackView());
            userCertificateStatusDTO.setIdcard_front_view(userPersonal.getIdcardFrontView());
        }

        return Response.success(userCertificateStatusDTO);
    }

    @GetMapping("/TestSetCertificate")
    @ApiOperation("跳过用户认证测试")
    public Response TestSetCertificate(String userKey) {
        FintechUser fintechUser = userDao.selectByPrimaryKey(userKey);
        boolean isAdd = false;
        if (fintechUser == null) {
            fintechUser = new FintechUser();
            fintechUser.setUserKey(userKey);
            isAdd = true;
        }
        fintechUser.setIsBaseInfoCertificate(1);
        fintechUser.setIsBaseInfoCertificate(1);
        fintechUser.setIsRealNameCertificate(2);
        fintechUser.setIsBankCardCertificate(1);
        fintechUser.setIsOperatorGet(1);
        fintechUser.setIsAllCertificate(1);
        fintechUser.setCertificate3(1);
        fintechUser.setCertificate4(1);
        if (isAdd) {
            fintechUser.setIsBaseInfo(0);
            fintechUser.setIsJob(0);
            fintechUser.setIsEmergency(0);
            userDao.insertSelective(fintechUser);
        } else {
            userDao.updateByPrimaryKey(fintechUser);
        }

        creditService.addCredit(userKey, 1000000L); //增加10000的额度

        return Response.success("成功");
    }

    @GetMapping("/getUserStatus")
    @ApiOperation("查询用户状态")
    //未认证=0  认证拒绝=1  认证通过=2  租赁中=3  逾期=4
    public Response getUserStatus(String userKey) {
        FintechCredit credit = creditService.getCreditDao().selectByPrimaryKey(userKey);
        if (credit == null || credit.getCreditStatus() == 0) {
            return Response.success("0");
        }
        if (credit.getCreditStatus() == 2) {
            return Response.success("1");
        }

        int count = rentBillHeaderService.getRentBillHeaderDao().selectCount("02", userKey);
        if (count > 0) {
            return Response.success("4");
        }

        count = rentBillHeaderService.getRentBillHeaderDao().selectCount("01", userKey);
        if (count > 0) {
            return Response.success("3");
        }

        return Response.success("2");
    }
}
